package myclient;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.StringTokenizer;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import choosebutton.DateChooserJButton;

/**
 * @author JIAO
 */
public class PersonalSettingFrame implements ActionListener{

	MainFrame mainFm = null;
	final Color c = new Color(238,238,238);
	/*
	 * 北京市 天津市 重庆市 上海市
	 * 河北省 山西省 辽宁省 吉林省 黑龙江省 江苏省 浙江省 安徽省 福建省 
	 * 江西省 山东省 河南省 湖北省 湖南省 广东省 海南省 四川省 贵州省 云南省 陕西省 甘肃省 青海省 台湾省
	 * */
	
	String[] province = new String[]{"北京市","天津市","重庆市","上海市","河北省","山西省","辽宁省","吉林省"};
	String[][] city = new String[][]{
			{"北京市"},
			{"天津市"},
			{"重庆市"},
			{"上海市"},
			{"石家庄","唐山","邯郸","秦皇岛","保定","张家口","承德","廊坊","沧州","衡水","邢台"},
			{"太原市","朔州市","原平市","大同市","阳泉市","榆次市","离石市","介休市","霍州市","潞城市","长治市","临汾市","高平市","侯马市","晋城市","河津市","运河市","永济市"},
			{"沈阳市","铁法市","铁岭市","阜新市","北票市","抚顺市","开远市","朝阳市","北宁市","辽阳市","本溪市","盘锦市","凌海市","凌源市","锦州市","鞍山市","葫芦岛市","大石桥市","瓦房店市兴城市","海城市","营口市","凤城市","盖州市","丹东市","东港市","庄河市","大连","普兰店市"},
			{"长春市","洮南市","大安市","松原市","榆树市","德惠市","舒兰市","九台市","白城市","吉林市","蛟河市","公主岭市","双辽市","四平市","辽源市","磐石市","延吉市","珲春市","梅河口市","图们市","龙井市","和龙市","白山市","临江市","通化市","集安市"},
	};
	HashMap<String,Integer> map = new HashMap<String, Integer>(); 
	private JComboBox cityCombo;
	private JComboBox provinceCombo;
	private JFrame setFrame;
	private JPanel cityPn;
	private JTextField IDText;
	private JTextField nameText;
	private JTextField mottoText;
	private JLabel photoLb;
	private JComboBox sexComboBox;
	private DateChooserJButton birthdayBt;
	final ImageFactory fac = new ImageFactory();
	public PersonalSettingFrame(final MainFrame mainFm) {
		this.mainFm = mainFm;
		setFrame = new JFrame();
		setFrame.setResizable(false);
		setFrame.setBounds(500, 500, 450, 450);
		
//		JLabel lb_personalSet = new JLabel("个人设置");
		
		for(int i=0;i<province.length;i++)
			map.put(province[i],i);
		
		//账号panel
		JPanel IDPn = new JPanel();
		IDPn.setPreferredSize(new Dimension(300,50));
		JLabel IDLb = new JLabel("账号：  ");
		IDText = new JTextField(15);
		IDText.setEnabled(false);
		IDText.setText(mainFm.myID);
		IDPn.add(IDLb);
		IDPn.add(IDText);
		
		//昵称panel
		JPanel namePn = new JPanel();
		namePn.setPreferredSize(new Dimension(300,50));
		JLabel nameLb = new JLabel("昵称： ");
		nameText = new JTextField(15);
		nameText.setText(mainFm.conn.name);
		namePn.add(nameLb);
		namePn.add(nameText);
		
		//个性签名panel
		JPanel mottoPn = new JPanel();
		System.out.println(mottoPn.getBackground().toString());
		mottoPn.setPreferredSize(new Dimension(300,50));
		JLabel mottoLb = new JLabel("个性签名： ");
		mottoText = new JTextField(20);
		mottoText.setText(mainFm.conn.motto);
		mottoPn.add(mottoLb);
		mottoPn.add(mottoText);
		
		//更改头像
		JPanel photoPn = new JPanel();
		photoLb = new JLabel("更改头像");
//		JOptionPane.showMessageDialog(null, mainFm.conn.pic);
		photoLb = new JLabel("更改头像",fac.make("/face/"+mainFm.conn.pic+"-1.gif"),0);
		photoLb.setName(mainFm.conn.pic);
		photoLb.setOpaque(true);
		photoLb.setPreferredSize(new Dimension(100,50));
		photoLb.addMouseListener(new MouseListener(){
			private JLabel[] photoLbs;
			public void mouseClicked(MouseEvent e) {
				final JFrame photoFm = new JFrame();
				photoFm.setBounds(200, 200, 400, 600);
				photoFm.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
				JButton bt_ok = new JButton("确定");
				JButton bt_close = new JButton("关闭");
				bt_close.addActionListener(new ActionListener(){
					public void actionPerformed(ActionEvent e) {
						photoFm.dispose();
					}});
				JPanel photoPn = new JPanel();
				photoPn.setLayout(new GridLayout(13 , 7));
				photoLbs = new JLabel[85];
				String s1 = "/face/";
				String s2 = "-1.gif";
				PhotoLbListener myPhotoLbListener = new PhotoLbListener();
				for(int i=1;i<86;i++){
					photoLbs[i-1] = new JLabel(fac.make(s1+i+s2));
					photoLbs[i-1].setName(i+"");
					photoLbs[i-1].setOpaque(true);
					photoLbs[i-1].addMouseListener(myPhotoLbListener);
					photoPn.add(photoLbs[i-1]);
				}
				JPanel btPn = new JPanel();
				btPn.add(bt_ok);
				btPn.add(bt_close);
				bt_ok.addActionListener(new ActionListener(){
					public void actionPerformed(ActionEvent e) {
						photoLb.setIcon(fac.make("/face/"+photoLb.getName()+"-1.gif"));
						photoFm.dispose();
					}});
				bt_close.addActionListener(new ActionListener(){
					public void actionPerformed(ActionEvent e) {
						photoFm.dispose();
					}});
				
				photoFm.getContentPane().setLayout(new BorderLayout());
				photoFm.getContentPane().add(photoPn,BorderLayout.CENTER);
				photoFm.getContentPane().add(btPn,BorderLayout.SOUTH);
				photoFm.setVisible(true);
				photoFm.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
				
			}
			public void mouseEntered(MouseEvent e) {
				photoLb.setBackground(Color.red);
			}
			public void mouseExited(MouseEvent e) {
				photoLb.setBackground(Color.gray);
			}
			public void mousePressed(MouseEvent e) {
			}
			public void mouseReleased(MouseEvent e) {
			}
			class PhotoLbListener implements MouseListener{
				public void mouseClicked(MouseEvent e) {
					int num = Integer.parseInt(photoLb.getName());
					photoLbs[num-1].setBackground(c);
					photoLb.setName(e.getComponent().getName());
					e.getComponent().setBackground(Color.lightGray);
				}
				public void mouseEntered(MouseEvent e) {
				}
				public void mouseExited(MouseEvent e) {
				}
				public void mousePressed(MouseEvent e) {
				}
				public void mouseReleased(MouseEvent e) {
				}
				
			}
		});
		photoPn.add(photoLb);
		photoPn.setPreferredSize(new Dimension(300,50));
		
		//性别panel
		JPanel sexPn = new JPanel();
		sexPn.setPreferredSize(new Dimension(300,50));
		JLabel sexLb = new JLabel("性别： ");
		sexComboBox = new JComboBox(new String[]{"女","男"});
		sexComboBox.setSelectedIndex(Integer.parseInt(mainFm.conn.sex));
		sexPn.add(sexLb);
		sexPn.add(sexComboBox);
		
		//生日panel
		JPanel birthdayPn = new JPanel();
		birthdayPn.setPreferredSize(new Dimension(300,50));
		JLabel birthdayLb = new JLabel("生日(单击更改)： ");
		birthdayBt = new DateChooserJButton(new SimpleDateFormat("yyyy-MM-dd"),mainFm.conn.birthday);
		birthdayPn.add(birthdayLb);
		birthdayPn.add(birthdayBt);
//		setFrame.getContentPane().add(birthday_pn);
		
		StringTokenizer tokens = new StringTokenizer(mainFm.conn.city," ");
		tokens.nextToken();
		String provinceStr = tokens.nextToken();
		String cityStr = tokens.nextToken();
		cityPn = new JPanel();
		JLabel cityLb = new JLabel("城市：");
		cityPn.setPreferredSize(new Dimension(300,50));
		provinceCombo = new JComboBox(province);
		provinceCombo.setSelectedItem(provinceStr);
		cityCombo = new JComboBox(city[map.get(provinceStr).intValue()]);
		cityCombo.setSelectedItem(cityStr);
		cityPn.add(cityLb);
		cityPn.add(provinceCombo);
		cityPn.add(cityCombo);
		provinceCombo.addActionListener(this);
		
		//button panel
		JPanel btPn = new JPanel();
		JButton btConfirm = new JButton("确定");
		JButton btCancel = new JButton("取消");
		btPn.add(btConfirm);
		btPn.add(btCancel);
		btConfirm.addActionListener(this);
		btCancel.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e) {
				setFrame.dispose();
			}});
		JPanel informationPn = new JPanel();
//		informationPn.setLayout(new GridLayout(7,1));
		
		informationPn.add(IDPn);
		informationPn.add(namePn);
		informationPn.add(mottoPn);
		informationPn.add(photoPn);
		informationPn.add(sexPn);
		informationPn.add(birthdayPn);
		informationPn.add(cityPn);
		
		setFrame.getContentPane().setLayout(new BorderLayout());
		setFrame.getContentPane().add(informationPn,BorderLayout.CENTER);
		setFrame.getContentPane().add(btPn,BorderLayout.SOUTH);
		
		setFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setFrame.setVisible(true);
	}
//	public static void main(String[] args){
//		new PersonalSettingFrame();
//	}
	public void actionPerformed(ActionEvent e) {
//		System.out.println(e.getActionCommand());
		if(e.getActionCommand().equals("确定")){
			PrintWriter writer = mainFm.conn.writer;
			writer.println("server");
			writer.println("set");
			//依次向服务器发送 id name motto pic sex birthday city
			writer.println(IDText.getText().trim());
			writer.println(nameText.getText().trim());
			writer.println(mottoText.getText());
			writer.println(photoLb.getName());
			writer.println(sexComboBox.getSelectedIndex());
//			System.out.println(sexComboBox.getSelectedIndex());
			writer.println(birthdayBt.getText());
			writer.println("中国 "+provinceCombo.getSelectedItem()+" "+cityCombo.getSelectedItem());
			System.out.println("更新完毕");
			setFrame.dispose();
			setFrame.dispose();
			return;
		}
		cityPn.remove(cityCombo);
		cityCombo = new JComboBox(city[provinceCombo.getSelectedIndex()]);
		System.out.println(provinceCombo.getSelectedIndex());
		cityPn.add(cityCombo);
		setFrame.setVisible(true);
		if(e.getActionCommand().equals(provinceCombo)){
			
		}else if(e.getActionCommand().equals(cityCombo)){
			
		}
		
	}

}
